package cn.tutu.blockchain.machinestatus.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.tutu.blockchain.common.TypeEnum;
import cn.tutu.blockchain.machinebackups.dao.MachineBackupsDao;
import cn.tutu.blockchain.machinebackups.entity.MachineBackups;
import cn.tutu.blockchain.machinestatus.dao.MachineStatusDao;
import cn.tutu.blockchain.machinestatus.entity.MachineStatus;
import cn.tutu.blockchain.machinestatus.service.MachineStatusService;

@Service
@Transactional
public class MachineStatusServiceImpl implements MachineStatusService {

	@Autowired
	private MachineStatusDao machineStatusDao;

	@Autowired
	private MachineBackupsDao machineBackupsDao;

	@Override
	public void create(MachineStatus machineStatus) {
		// 根据矿机ID 文件哈希值 查找是否已存在，如果存在则修改最后更新时间，否则直接添加
		List<MachineStatus> statusList = null;
		if (machineStatus.getType().equals(TypeEnum.MACHINE_ROOT_HASH.getCode())) {
			MachineStatus ms = new MachineStatus();
			ms.setMinemachineId(machineStatus.getMinemachineId());
			ms.setMinemachineId(TypeEnum.MACHINE_ROOT_HASH.getCode());
			statusList = machineStatusDao.findByModel(ms);
		} else {
			statusList = machineStatusDao.findByModel(machineStatus);
		}
		if (statusList.isEmpty()) {
			machineStatusDao.insert(machineStatus);
			MachineBackups machineBackups = new MachineBackups();
			machineBackups.setBackupCount(0);
			machineBackups.setMachineStatusId(machineStatus.getId());
			machineBackupsDao.insert(machineBackups);
		} else {
			MachineStatus macStatus = statusList.get(0);
			// 设置状态为有效
			macStatus.setStatus(TypeEnum.STATUS_Y.getCode());
			if (machineStatus.getType().equals(TypeEnum.MACHINE_ROOT_HASH.getCode())) {
				macStatus.setFileHash(machineStatus.getFileHash());
			}
			machineStatusDao.update(statusList.get(0));
		}

	}

	@Override
	public List<String> getFileHashByMacId(String machineId) {

		synchronized (this) {
			// 根据矿机ID获取非本矿机并且备份次数少于2次的文件
			List<MachineBackups> macList = machineStatusDao.findFileHashByMacIdNot(machineId);

			// 不需要在当前矿机备份的文件
			List<MachineBackups> removeList = new ArrayList<MachineBackups>();

			List<String> hashList = new ArrayList<String>();
			for (MachineBackups backup : macList) {
				backup.setBackupCount(backup.getBackupCount() + 1);
				if (backup.getBackupCount() == 1) {
					// 设置备份矿机一
					backup.setMachineBackupsId1(machineId);
				} else if (backup.getBackupCount() == 2) {
					// 如果该矿机已备份过该文件则移除
					if (machineId.equals(backup.getMachineBackupsId1())) {
						removeList.add(backup);
						continue;
					}
					// 设置备份矿机二
					backup.setMachineBackupsId2(machineId);
				}
				hashList.add(backup.getFileHash());
			}
			macList.removeAll(removeList);
			if ((macList != null) && (macList.size() > 0)) {
				machineBackupsDao.updateBatch(macList);
			}
			return hashList;
		}
	}

	@Override
	public String getRootHashByMacId(String machineId) {
		MachineBackups rootHash = machineStatusDao.findRootHashByMacId(machineId);
		return rootHash == null ? null : rootHash.getFileHash();
	}

	@Override
	public List<MachineStatus> getByLastUpdateDate() {
		// TODO Auto-generated method stub
		return null;
	}

}
